// pages/register/register.js
import {
  ApiRootUrl,
  apiList
} from '../../../config/index.js'
import Toast from '../../../@vant/weapp/toast/toast';
import Dialog from '../../../@vant/weapp/dialog/dialog';
import {
  IMG_PATH
} from '../../../config/index.js'
import {
  request,login
} from "../../../utils/util.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    roleObj:{
      '0':'待面试人员',
      '1':'在职人员',
      '2':'管理人员',
      '3':'离职人员',
      '4':'游客',
    },
    roleListFlag:false,
    roleList:[{text:'用户1',id:'1'},{text:'用户2',id:'2'}],
    jobNumber:'',
    showcertifyType:false,
    certifyTypecolumns:['身份证号','员工工号'],
    certifyTypeDesc:'身份证号',
    isRegisterFlag: false,
    isLogin: false,
    position: 'left',
    message:"会话结束,请重新登录小程序",
    show:false,
    getPhoneNumberFlag: true,
    serviceCompany: '请选择',
    serviceCode: '',
    chooseMulitUser:false,
    showRole: false,
    showService: false,
    rolecolumns: [],
    servicecolumns: [],
    roleId: '',
    IMG_PATH: IMG_PATH,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    username: '',
    phone: '',
    role: '请选择',
    company: '请选择',
    identifycode: '',
    showcompany: false,
    tishiShow: false,
    columns: [],
    roleIdCode:'',
    orgCode: '',
    roleCode: '',
    count:3,
    canLogin:false,
    labourSideColumns:[],  //驻厂代表角色集合
    columnsUser:[
    ],
    result:[],
    tapIndex:0,
    result1:"",
    editusername:false,
    editidentity:false,
    showUserList:false,
    userList:[],
  },
  roleListClose(){
    Toast("请选择角色！")
  },
  roleListconfirm(event){
    console.log("event----",event)
    // this.setData({
    //   roleListFlag:false,
    //   roleIdCode:event.detail.value.id
    // })
    let obj = event.detail.value.obj;
    this.setData({
      roleListFlag:false
    })
    this.checkdoLoginMethod(obj)
  },
  certifyTypeClose(){
    this.setData({
      showcertifyType:false
    })
  },
  certifyTypeConfirm(event){
    this.setData({
      certifyTypeDesc:event.detail.value,
      showcertifyType:false
    })
  },
  oncertifyTypeChange(){
    this.setData({
      showcertifyType:true
    })
  },
  onChangeLabel(event) {
    console.log("roleCode",this.data.labourSideColumns[event.detail] )

    this.setData({
      result1: event.detail,
      roleCode:this.data.labourSideColumns[event.detail].code,
      result:[this.data.labourSideColumns[event.detail].id]
    });
  },
  onChangeUser(event){
    const { picker, value, index } = event.detail;
  },
  onConfirmUser(event){
    const { picker, value, index } = event.detail;
    console.log(value,index)
    if(value.length == 0){
      Toast("请选择用户")
      return
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    request(apiList.doLoginAppManagerUser+`?openId=${value.openId}&labourCode=${value.laobourCode}&componentCode=${value.componentCode}`,
    {},"GET").then(res =>{
      Toast.clear();
      console.log(res)
      let data1 = res.data
      wx.setStorageSync('userinfo', data1)
      // 无user表示是劳务工
      if (!data1.user) {
        let type  = 0;
        if(data1.labourCode == null){
          type = 1
        }
        wx.redirectTo({
          url: `/pages/firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`
        })
      } else {
        console.log("data.user===>", data1.user)
        // 有user表示是非劳务工
        if (data1.user.typeCode == 'business') {
          wx.redirectTo({
            url: '../companyfirstpage/companyfirstpage',
          })
        } else if (data1.user.typeCode == 'labour') {
          wx.redirectTo({
            url: '../laborcompany/firstpage/firstpage',
          })
        } else if (data1.user.typeCode == 'society') {
          wx.redirectTo({
            url: '/pages/laborcompany/firstpage/firstpage'
          });
        }
      }
    }).catch(err =>{
      Toast.clear();
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
        mask:true
      });
    })
    return
    this.setData({
      chooseMulitUser:false
    })
  },
  onCloseUserPopup(){
    this.setData({
      chooseMulitUser:false
    })
  },
  usernameInput(event) {
    this.setData({
      username: event.detail
    })
  },
  usernameBlur(event) {
    this.setData({
      username: event.detail.value
    })
  },
  identifycodeInput(event) {
    this.setData({
      identifycode: event.detail
    })
  },
  identifycodeBlur(event) {
    this.setData({
      identifycode: event.detail.value
    })
  },
  jobNumberInput(event) {
    console.log(event)
    this.setData({
      jobNumber: event.detail
    })
  },
  jobNumberBlur(event) {
    console.log(event)
    this.setData({
      jobNumber: event.detail.value
    })
  },
  getPhoneNumber(e) {
    let self = this;
    let flag = true;
    wx.checkSession({
      success: function (res) {
        console.log('session_key未过期')
        //session_key未过期
        flag = true;
      },
      fail: function (res) {
        flag = false;
        self.setData({
          tishiShow: true
        })
      }
    })
    if (!flag) {
      return;
    }
    let openId = wx.getStorageSync("openId");
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      let url = `${ApiRootUrl}/index/grantPhone`;
      let params = {
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      }
      Toast.loading({
        message: '加载中...',
        mask: true,
        duration: 0,
        forbidClick: true,
      });
      wx.request({
        url: url,
        method: 'GET',
        header: {
          'content-type': 'application/json; charset=utf-8',
          'cookie': wx.getStorageSync("sessionid")
        },
        params: params,
        data: params,
        success(res) {
          Toast.clear();
          if (res.data.code == 20000) {
            let phoneNumber = res.data.data;
            wx.setStorageSync("phoneNumber", phoneNumber);
            self.setData({
              phone: phoneNumber,
              getPhoneNumberFlag: false
            })
          } else if (res.data.code == 30000) {
            self.setData({
              tishiShow: true
            })
          } else {
            Toast({
              duration: 3000,
              forbidClick: true,
              type: 'fail',
              message: res.data.message,
              mask:true
            });
          }
        },
        fail(res) {
          Toast.clear();
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: '网络已断开',
          });
        }
      })
    } else {
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: '不授权将无法登录该小程序！',
      });
    }
  },
  roleClick() {
    let that = this
    if(this.data.type == 6){
      this.setData({
        showRole:true
      })
      return
    }
    wx.showActionSheet({
      itemList: ['非驻厂代表', '驻厂代表',],
      success (res) {
        if(res.tapIndex == 0){
          that.setData({
            showRole:true,
            tapIndex:0
          })
        }
        if(res.tapIndex == 1){
          that.setData({
            showRoleSide:true,
            tapIndex:1
          })
        }
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  roleCloseSide(){
    this.setData({
      showRoleSide:false
    })
  },
  roleConfirmSide(){
    if(this.data.result.length == 0){
      this.setData({
        showRoleSide:false
      })
    }else{
      this.setData({
        role:"驻厂代表",
        showRoleSide:false
      })
    }
  },
  serviceClick() {
    this.setData({
      showService: true
    })
  },
  serviceClose() {
    this.setData({
      showService: false
    })
  },
  roleConfirm(event) {
    console.log(event);
    let ids = []
    ids.push(event.detail.value.id)
    this.setData({
      showRole: false,
      role: event.detail.value.text,
      roleId: event.detail.value.id,
      result:ids,
      roleCode: event.detail.value.code
    })
  },
  serviceConfirm(event) {
    console.log(event)
    this.setData({
      serviceCode: event.detail.value.id,
      serviceCompany: event.detail.value.text,
      showService: false
    })
  },
  roleClose() {
    this.setData({
      showRole: false
    })
  },
  checkIdentity(){
    if (!this.data.username || !this.data.username.length) {
      Toast('请输入姓名！');
      return;
    }
    if(!this.data.identifycode || !this.data.identifycode.length){
      Toast('请输入身份证号！');
      return;
    }
    let self=this
    return new Promise((reslove,reject)=>{
      let identifycode=this.data.identifycode.trim().toUpperCase()
      if(identifycode.length==17){
        identifycode=identifycode+'X'
      }
      let params = {
        name:this.data.username.trim(),
        identity:identifycode,
      }
      request(apiList.checkByPerson,params).then(res =>{
        self.submitClick()
      }).catch(err => {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: err.message,
          mask:true
        });
      })
    })
  },
  isCanbaoConfirm(e){
    let self=this
    console.log(e);
    this.newLogin(e.detail.value)
    let data=e.detail.value
    if (!data.user) {
      wx.redirectTo({
        url: `/pages/firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`,
      })
    }else{
       // 有user表示是非劳务工
       if (data.user.typeCode == 'business') {
        wx.redirectTo({
          url: '/pages/companyfirstpage/companyfirstpage',
        })
      } else if (data.user.typeCode == 'labour') {
        wx.redirectTo({
          url: '/pages/laborcompany/firstpage/firstpage',
        })
      }
    }
  },
  isCanbaoCancel(){
    let self=this
    this.setData({
      showUserList:false
    })
    wx.showModal({
      title: '提示',
      content: '您已注册过心园通小程序,是否继续注册?',
      cancelText:"选择角色",
      confirmText:"继续注册",
      success (res) {
        if (res.confirm) {
        
        } else if (res.cancel) {
          self.setData({
            showUserList:true
          })
        }
      }
    })
  },
  closeUserList(){
    let self=this
    this.setData({
      showUserList:false
    })
    wx.showModal({
      title: '提示',
      content: '您已注册过心园通小程序,是否继续注册?',
      cancelText:"选择角色",
      confirmText:"继续注册",
      success (res) {
        if (res.confirm) {
      
        } else if (res.cancel) {
          self.setData({
            showUserList:true
          })
        }
      }
    })
  },
  // 提交确认
  submitClick() {
    if (!this.data.canLogin) {
      Toast('请先阅读隐私协议');
      return;
    }
    if (this.data.isLogin) {
      Toast('该二维码无效，请退出重新扫码!');
      return;
    }
    let self = this;
    let code = wx.getStorageSync('code');
    if (!this.data.username || !this.data.username.length) {
      Toast('请输入姓名！');
      return;
    }
    if (!this.data.phone || !this.data.phone.length) {
      Toast('请点击授权！');
      return;
    }
    if(!this.data.identifycode || !this.data.identifycode.length){
      Toast('请输入身份证号！');
      return;
    }
    if(!this.data.jobNumber || !this.data.jobNumber.length){
      Toast('请输入所属公司！');
      return;
    }
    Toast.loading({
      duration: 0,
      mask: true,
      message: '操作中...',
      forbidClick: true,
    });
    let identifycode=this.data.identifycode.trim().toUpperCase()
    if(identifycode.length==17){
      identifycode=identifycode+'X'
    }
    let params = {
      name: this.data.username,
      phone: this.data.phone,
      identity: identifycode,
      openId: wx.getStorageSync('openId'),
      originComponentName:this.data.jobNumber,
      descOrgCode:this.data.code,
      componentType:9,
      descComponentName:this.data.name
    }
    console.log("params==>",params)
    wx.request({
      url: ApiRootUrl + '/index/newScanOutsourceRegister',
      method: 'post',
      data: params,
      params: params,
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        console.log("res======>", res)
        Toast.clear();
        if (res.data.code == 20000) {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'success',
            message: '注册成功！',
          });
          wx.setStorageSync('isRegisterFlag', true);
          let data = res.data.data;
          self.newLogin(data)
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
          setTimeout(()=>{
            self.newCheckUser()
          },3000)
        }
      },
      fail(res) {
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络错误',
        });
      }
    })
  },
  // 公司选择确认
  companyConfirm(event) {
    console.log(event)
    this.setData({
      showcompany: false,
      company: event.detail.value.text,
      orgCode: event.detail.value.id,
    })
  },
  companyClose() {
    this.setData({
      showcompany: false
    })
  },
  // 公司选择取消
  companyCancel() {
    this.setData({
      showcompany: false
    })
  },
  companyClick(event) {
    this.setData({
      showcompany: true
    })
    console.log(event)
  },
  //劳务公司驻场代表能服务的企业
  initService() {
    let self = this;
    let orgCode = this.data.code;
    wx.request({
      url: `${ApiRootUrl}/labor/queryBusinessList/${orgCode}`,
      method: 'get',
      success(res) {
        if (res.data.code == 20000) {
          let servicecolumns = [];
          res.data.data.forEach((item) => {
            servicecolumns.push({
              id: item.orgCode,
              text: item.companyName
            })
          })
          self.setData({
            servicecolumns: servicecolumns
          })
          console.log(res.data.data);
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络错误',
        });
      }
    })
  },
  // 初始化公司list ---获取注册绑定角色的列表
  initCompany() {
    let self = this;
    let orgCode = this.data.code;
    wx.request({
      url: `${ApiRootUrl}/index/queryUserListByOrgCode?orgCode=${orgCode}&componentType=${this.data.type}`,
      method: 'get',
      success(res) {
        if (res.data.code == 20000) {
          let rolecolumns = [];
          let labourSideColumns = [];
          res.data.data.forEach((item) => {
            if(item.roleCode == "labourSite"){
              labourSideColumns.push({
                id: item.id,
                text: `${item.accountName}-${item.nickName}-${item.roleName}`,
                code: item.roleCode,

              })
            }else{
              rolecolumns.push({
                id: item.id,
                text: `${item.accountName}-${item.nickName}-${item.roleName}`,
                code: item.roleCode
              })
            }
            
          })
          self.setData({
            rolecolumns: rolecolumns,
            labourSideColumns:labourSideColumns,
          })
          console.log(res.data.data);
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络错误',
        });
      }
    })
  },
  // checkIdentity(){
  //   return new Promise((reslove,reject)=>{
  //     let identifycode=this.data.identifycode.trim().toUpperCase()
  //     if(identifycode.length==17){
  //       identifycode=identifycode+'X'
  //     }
  //     let params = {
  //       name:this.data.username.trim(),
  //       identity:identifycode
  //     }
  //     request(apiList.checkByPerson,params).then(res =>{
  //       reslove()
  //     }).catch(err => {
  //       Toast({
  //         duration: 3000,
  //         forbidClick: true,
  //         type: 'fail',
  //         message: err.message,
  //       });
  //     })
  //   })
  // },
  checkRegister() {
    let self = this;
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/index/checkWorkInfo`;
    let params = {
      openId:openId
    }
    Toast.loading({
      message: '加载中...',
      mask: true,
      duration: 0,
      forbidClick: true,
    });
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      params:params,
      data:params,
      success(res) {
        Toast.clear();
        console.log("res=========>",res)
        if (res.data.code == 20000) {
          let data = res.data.data;
          if(data.length > 1){
            let list = []
            data.forEach((item)=>{
              list.push({
                id:item.userType,
                obj:item,
                text:item.componentName+"-"+self.data.roleObj[item.userType],
              })
            })
            self.setData({
              roleListFlag:true,
              roleList:list
            })
          }else if(data.length == 1){
            let params = res.data.data[0];
            self.checkdoLoginMethod(params);
          }
          
        } else if (res.data.code == 30001) {
          
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  newCheckUser() {
    let self = this;
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/index/newCheckUser?openId=${openId}`;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res){
        // Toast.clear();
        wx.hideLoading()
        console.log("res====>111111",res)
        if (res.data.code == 20000) {
        console.log('注册过小程序',res.data.data);
        let userList=res.data.data
        userList.forEach((item)=>{
          if(item.userType==0){
            item.text='待面试-'+item.componentName
          }
          if(item.userType==1){
            item.text='在职务工人员-'+item.componentName
          }
          if(item.userType==2&&item.componentName){
            item.text='在职管理人员-'+item.componentName
          }
          if(item.userType==2&&!item.componentName){
            item.text=item.user.roleName
          }
          if(item.userType==3){
            item.text='离职务工人员-'+item.componentName
          }
          if(item.userType==4){
            item.text='游客'
          }
          if(item.userType==5){
            item.text='外协-'+item.componentName
          }
          if(item.userType==7){
            item.text='未入职申请务工人员-'+item.componentName
          }
        })
        wx.showModal({
          title: '提示',
          content: '您已注册过心园通小程序,是否继续注册?',
          cancelText:"选择角色",
          confirmText:"继续注册",
          success (res) {
            if (res.confirm) {
              
            } else if (res.cancel) {
              self.setData({
                userList:userList,
                showUserList:true
              })
            }
          }
        })
        }  else {
          // Toast({
          //   duration: 3000,
          //   forbidClick: true,
          //   type: 'fail',
          //   message: res.data.message,
          // });
        }
        // Toast.clear();
      },
      fail(res) {
        // Toast.clear();
        wx.hideLoading()
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  doLoginMethod(params){
    let self = this;
    wx.request({
      url: `${ApiRootUrl}/index/doLoginApp`,
      method: 'POST',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      success(res) {
        if(res.data.code == 20000){
          wx.setStorageSync('userinfo',res.data.data)
          console.log('res.data.data-----',res.data.data);
          let data1 = res.data.data;
          // 无user表示是劳务工
          if (!data1.user) {
            let type  = 0;
            if(data1.labourCode == null){
              type = 1
            }
            wx.redirectTo({
              url: `/pages/firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`,
            })
          } else {
            console.log("data.user===>", data1.user)
            // 有user表示是非劳务工
            if (data1.user.typeCode == 'business') {
              wx.redirectTo({
                url: '../companyfirstpage/companyfirstpage',
              })
            } else if (data1.user.typeCode == 'labour') {
              wx.redirectTo({
                url: '../laborcompany/firstpage/firstpage',
              })
            } else if (data1.user.typeCode == 'society') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage'
              });
            }
          }
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res){
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  newLogin(params){
    Toast.loading({
      duration: 0,
      message: '登录中...',
      forbidClick: true,
    });
    let self = this;
    wx.request({
      url: `${ApiRootUrl}/index/newLogin`,
      method: 'POST',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      success(res) {
        Toast.clear();
        if(res.data.code == 20000){
         wx.setStorageSync('userinfo', res.data.data)
         let data1 = res.data.data;
          // 无user表示是劳务工
          if (!data1.user) {
            let type  = 0;
            if(data1.labourCode == null){
              type = 1
            }
            wx.redirectTo({
              url: `/pages/firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`
            })
          } else {
            console.log("data.user=111==>", data1.user)
            // 有user表示是非劳务工
            if (data1.user.typeCode == 'business') {
              wx.redirectTo({
                url: '/pages/companyfirstpage/companyfirstpage'
              });
            } else if (data1.user.typeCode == 'labour') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage'
              });
            } else if (data1.user.typeCode == 'society') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage'
              });
            }
          }
        }else{
         
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res){
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  checkdoLoginMethod(params){
    wx.request({
      url: `${ApiRootUrl}/index/doLoginApp`,
      method: 'POST',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      data:params,
      success(res) {
        if(res.data.code == 20000){
          wx.setStorageSync('userinfo',res.data.data)
          console.log('res.data.data-----',res.data.data);
          let data1 = res.data.data
          // 无user表示是劳务工
          if (!data1.user) {
            let type  = 0;
            if(data1.labourCode == null){
              type = 1
            }
            wx.redirectTo({
              url: `/pages/firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`
            })
          } else {
            console.log("data.user===>", data1.user)
            // 有user表示是非劳务工
            if (data1.user.typeCode == 'business') {
              wx.redirectTo({
                url: '/pages/companyfirstpage/companyfirstpage',
              })
            } else if (data1.user.typeCode == 'labour') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage',
              })
            } else if (data1.user.typeCode == 'society') {
              wx.redirectTo({
                url: '/pages/laborcompany/firstpage/firstpage'
              });
            }
          }
        }else{
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
            mask:true
          });
        }
      },
      fail(res){
        console.log(res)
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  toOpen(){
    this.timer && clearInterval(this.timer)
    this.timer = setInterval(() => {
      let count = this.data.count
      if(count <= 0){
        clearInterval(this.timer)
        this.setData({
          count:count
        })
      }else{
        this.setData({
          count:count - 1
        })
      }
    }, 1000);
    if(this.data.canLogin){
      this.setData({
        canLogin:false
      })
    }else{
      this.setData({
        show:true
      })
    }
    
  },
  onClose(){
    this.timer && clearInterval(this.timer)
    this.setData({
      show:false
    })
  },
  setPrativy(){
    this.setData({
      canLogin:true,
      show:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   * 
   */
  onLoad: function (options) {
    console.log("options====>", options)
    wx.setStorageSync('qrcodeInfo', options)
    let code = options.OrgCode;
    let name = options.orgName;
    let type = options.componentType;
    let obj = {
      name: name,
      code: code
    }
    app.globalData.labourInfo = obj;
    let openId = wx.getStorageSync('openId')
    login().then(_=>{
      this.getUserName()
      this.newCheckUser()
      // this.checkRegister();
    })
    this.setData({
      code: code,
      name: name,
      type: type
    })
    //获取注册所需要的参数
    if (Number(type) == 5) {
      this.initService();
      // this.initCompany();
    } else if (Number(type) == 6) {
      this.getLabourList(code)
    }
    this.initCompany();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getUserName() {
    let self=this
    let openId = wx.getStorageSync("openId");
    let url = `${ApiRootUrl}/index/newReloadInfo?openId=${openId}`;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res){
        wx.hideLoading()
        console.log("res====>111111",res)
        if (res.data.code == 20000) {
          if(res.data.data){
            self.setData({
          username:res.data.data.name,
          identifycode:res.data.data.identity,
          editidentity:true,
          editusername:true
       })
          }
         
       
       
        } else {
    
        }
      },
      fail(res) {
        wx.hideLoading()
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络已断开',
        });
      }
    })
  },
  //获取企业下属的劳务公司
  getLabourList(code) {
    request(apiList.getLabourList + "?componentCode=" + code, {}, "GET").then(res => {
      let columns = [];
      res.data.forEach((item) => {
        columns.push({
          id: item.orgCode,
          text: item.labourName
        })
      })
      console.log("columns==>",columns,res)
      let obj = {
        id:"",
        text:"全部"
      }
      columns.unshift(obj)
      app.globalData.companyList = columns
    }).catch(err => {
      Toast({
        duration: 3000,
        forbidClick: true,
        type: 'fail',
        message: err.message,
        mask:true
      });
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})